<template>
<div>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" width="800px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="分组名称" prop="ryfzMc">
        <el-input v-model="dataForm.ryfzMc" placeholder="请输入分组名称" style="width:90%;"></el-input>
      </el-form-item>

      <el-form-item label="分组系数" prop="fzXs">
        <el-input-number v-model="dataForm.fzXs" :precision="2" :step="0.01" :max="500" :min="0"  placeholder="请输入分组系数"  style="width:90%;" ></el-input-number>
      </el-form-item>

      <el-form-item label="分组说明" prop="ryfzSm">
        <el-input type="textarea" :rows="4" maxlength="150" show-word-limit v-model="dataForm.ryfzSm" placeholder="请输入分组说明" style="width:90%;"></el-input>
      </el-form-item>
      <el-form-item v-if="!dataForm.id" label="人员编号" prop="ryCode">
        <el-input type="textarea" :rows="4" maxlength="150" show-word-limit @blur="renCode" v-model="dataForm.ryCode" placeholder="人员编号" style="width:90%;"></el-input>
      </el-form-item>
      <el-form-item v-if="!dataForm.id"  label="人员姓名" prop="ryNames">
        <el-input type="textarea" :rows="4" maxlength="150" show-word-limit v-model="dataForm.ryNames" readonly placeholder="人员姓名" style="width:90%;"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
  <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="70%"
  :close-on-click-modal="false"
  append-to-body
  >
  <div>
    <el-table :data="renCodeList" ref="table" border
                  class="avue-crud">
          <el-table-column type="index" header-align="center" align="center" label="序号" width="80"
                           ></el-table-column>
          <el-table-column prop="ryName" header-align="center" align="center" label="姓名"></el-table-column>
          <el-table-column prop="ryCode" header-align="center" align="center" label="人员编号"></el-table-column>
          <el-table-column prop="ryZw" min-width="150px" header-align="center" align="center" label="职位"></el-table-column>
          <el-table-column label="操作" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="del(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
    </el-table>
  </div>
  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="queD">确 定</el-button>
    <el-button @click="close">取 消</el-button>
  </span>
</el-dialog>
</div>
</template>

<script>
/*!
 * 绩效考核-人员分组维护子页面-增加、修改分组
 *
 * @author zmk
 * @date 2021-08-03 10:11:35
 */
import {getObj, addObj, putObj,getRyxxbBatchByCode,saveFzDetail} from '@/api/jiangjin/jiangjinryfzb'

export default {
  data() {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        id: 0,
        jjDeptId: 0,
        ryfzMc: '',
        ryfzSm: '',
        ryfzLx: 1,
        deptId: 0,
        fzXs:'',
        ryCode:"",
        ryNames:""
      },
      dataRule: {
        ryfzMc: [
          {required: true, message: '分组名称不能为空', trigger: 'blur'},
          {min: 0, max: 50, message: '长度需在 50 个字符以内', trigger: 'blur'}
        ],
        fzXs: [
          {required: true, message: '分组名称不能为空', trigger: 'blur'},
        ]
      },
      dialogVisible:false,
      renCodeList:[]
    }
  },
  methods: {
    csh() {
      this.dataForm = {
        id: 0,
        jjDeptId: 0,
        ryfzMc: '',
        ryfzSm: '',
        fzXs:'',
        ryfzLx: 1,
        deptId: 0,
        ryCode:"",
        ryNames:""
      };
    },
    init(deptId, id) {
      this.csh();
      this.renCodeList=[]
      this.dataForm.id = id || 0;
      this.dataForm.jjDeptId = deptId;
      this.dataForm.deptId = deptId;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              console.log(JSON.stringify(data))
              if(data.data.code==0){
                //data.data.data.id
                if(this.renCodeList.length>0){
                  let userCodes=this.renCodeList.map(item=>{
                    return item.ryCode
                  })
                  var params={
                    id:data.data.data.id,
                    userCodes:userCodes.toString()
                  }
                  saveFzDetail(params).then(res=>{
                    if(res.data.code==0){
                      this.$notify.closeAll();
                      this.$notify.success('添加成功')
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                }else{
                  this.$notify.closeAll();
                  this.$notify.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }

              }

            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //人员编号
    renCode(){
      if(this.dataForm.ryCode){
        getRyxxbBatchByCode(this.dataForm.ryCode).then(res=>{
          this.dialogVisible=true
          this.renCodeList=res.data.data
        })
      }

    },
    queD(){
      var names=[]
       names=this.renCodeList.map(item=>{
         return item.ryName
      })
      this.$set(this.dataForm,'ryNames',names.toString())
      this.dialogVisible=false
    },
    close(){
      this.renCodeList=[]
      this.dialogVisible=false
    },
    //删除
    del(index){
      this.renCodeList.splice(index,1)
    },
  }
}
</script>
